package com.ruoyi.system.controller.health;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginWxUser;
import com.yxzz.core.domain.TUser;
import com.yxzz.core.domain.TUserPatientRela;
import com.yxzz.core.service.ITUserPatientRelaService;
import com.yxzz.core.service.ITUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 客户信息Controller
 *
 * @author ruoyi
 * @date 2023-03-12
 */
@RestController
@RequestMapping("/wxuser")
public class TUserController extends BaseController
{
    @Autowired
    private ITUserService tUserService;

    @Autowired
    private ITUserPatientRelaService tUserPatientRelaService;

    /**
     * 查询客户信息列表
     */
    @RequiresPermissions("health:user:list")
    @GetMapping("/list")
    public TableDataInfo list(TUser tUser)
    {
        startPage();
        List<TUser> list = tUserService.selectTUserList(tUser);
        return getDataTable(list);
    }

    /**
     * 导出客户信息列表
     */
    @RequiresPermissions("health:user:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TUser tUser)
    {
        List<TUser> list = tUserService.selectTUserList(tUser);
        ExcelUtil<TUser> util = new ExcelUtil<TUser>(TUser.class);
        util.exportExcel(response, list, "客户信息数据");
    }

    /**
     * 获取客户信息详细信息
     */
    @RequiresPermissions("health:user:query")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(tUserService.selectTUserByUserId(userId));
    }

    /**
     * 注册用户信息
     */
    @InnerAuth
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody TUser tuser)
    {
        return R.ok(tUserService.insertTUser(tuser) > 0);
    }


    /**
     * 获取客户信息详细信息
     */
    @InnerAuth
    @GetMapping("/openId/{openId}")
    public R<LoginWxUser> getInfoByOpenId(@PathVariable("openId") String openId)
    {

        TUser tUser = tUserService.selectTUserByOpenId(openId);
        if (StringUtils.isNull(tUser))
        {
            return R.fail("不存在tUser");
        }
        LoginWxUser userVo = new LoginWxUser();
        userVo.settUser(tUser);

        TUserPatientRela tUserPatientRela = new TUserPatientRela();
        tUserPatientRela.setUserId(tUser.getUserId());
        List<TUserPatientRela> relationshipList = tUserPatientRelaService.selectTUserPatientRelaList(tUserPatientRela);

        Set<Long> ids = new HashSet<Long>();
        for (TUserPatientRela rela : relationshipList){
            ids.add(rela.getPatientId());
        }
        userVo.setPatientIds(ids);



        return R.ok(userVo);
    }


    /**
     * 新增客户信息
     */
    @RequiresPermissions("health:user:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TUser tUser)
    {
        return toAjax(tUserService.insertTUser(tUser));
    }

    /**
     * 修改客户信息
     */
    @RequiresPermissions("health:user:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TUser tUser)
    {
        return toAjax(tUserService.updateTUser(tUser));
    }

    /**
     * 删除客户信息
     */
    @RequiresPermissions("health:user:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(tUserService.deleteTUserByUserIds(userIds));
    }
}
